package com.example.mybatisplus.controller;

import com.example.mybatisplus.service.SingleTimeService;
import com.example.mybatisplus.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.AskConsultService;
import com.example.mybatisplus.model.domain.AskConsult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-05-31
 * @version v1.0
 */
@Controller
@RequestMapping("/api/askConsult")
public class AskConsultController {

    private final Logger logger = LoggerFactory.getLogger( AskConsultController.class );

    @Autowired
    private AskConsultService askConsultService;
    @Autowired
    private UserService userService;
    @Autowired
    private SingleTimeService singleTimeService;
//    /**
//    * 描述：根据Id 查询
//    *
//    */
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    @ResponseBody
//    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
//        AskConsult  askConsult =  askConsultService.getById(id);
//        return JsonResponse.success(askConsult);
//    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        askConsultService.removeById(id);
        return JsonResponse.success(null);
    }


//    /**
//    * 描述：根据Id 更新
//    *
//    */
//    @RequestMapping(value = "", method = RequestMethod.PUT)
//    @ResponseBody
//    public JsonResponse updateAskConsult(AskConsult askConsult) throws Exception {
//        askConsultService.updateById(askConsult);
//        return JsonResponse.success(null);
//    }


    /**
    * 描述:创建AskConsult
    *
    */
    @RequestMapping(value = "/askConsult", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestBody AskConsult  askConsult) throws Exception {
        askConsultService.add(askConsult);
        return JsonResponse.success("添加成功");
    }

    /**
     * 获取全部数据
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getAll() throws Exception {
        List<AskConsult> askConsultList = askConsultService.getAll();
        List<Map<String,String>> list=new ArrayList<>();
        for (AskConsult askConsult:askConsultList){
            list.add(packgeAskConsult(askConsult));
        }
        return JsonResponse.success().setData(list);
    }

    /**
     * 根据学生stuId删除（假）
     */
    @RequestMapping(value = "/deleteChange", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse deleteChange(Long stuId) throws Exception{
        askConsultService.deleteChange(stuId);
        return JsonResponse.success(null);
    }


    public Map<String,String> packgeAskConsult(AskConsult askConsult){
        Map<String,String> map=new HashMap<String, String>();
        try {
            map.put("stu_name",userService.getById(askConsult.getStuId()).getStuName());
            map.put("week_name",singleTimeService.getById(askConsult.getTimeId()).getWeakDay());
            map.put("detail_time",singleTimeService.getById(askConsult.getTimeId()).getTimeDay());
            map.put("id",askConsult.getId().toString());
            map.put("askReason",askConsult.getAskReason());
            map.put("stuId",askConsult.getStuId().toString());
        } catch (Exception e){
            return map;
        }
        return map;
    }
}

